/*
 * @Title: 九宫格抽奖
 * @Author: JSL
 * @Date:   2018-12-21
 * @Last Modified by:   JSL
 * @Last Modified time: 2018-12-22
 */

var dataInfo = new Object(),
  template = {
    init: function() {
      dataInfo.init = new Vue({
        el: '#app',
        data: {
          newsList: [],
          number: 0,
          prizeInfo: [],
          prizeAllInfo: {
            imgUrl: '',
            reward: '',
            type: '',
          },
          prizeStatus: 0,
          prizeSwitch: 1, //1 - 可抽奖  2 - 抽奖中 请等待
          initStatus: getQueryString('initStatus') == 2 ? 2 : 0, //0隐藏 1抽奖次数用完 2中奖信息确认
          step: [ //转速控制
            ['c1', 0], // 第一个元素特殊设置
            ['c1', 500],
            ['c2', 400],
            ['c3', 300],
            ['c4', 200],
            ['c5', 200],
            ['c6', 200],
            ['c7', 200],
            ['c8', 200],
            ['c1', 100],
            ['c2', 100],
            ['c3', 100],
            ['c4', 100],
            ['c5', 100],
            ['c6', 100],
            ['c7', 100],
            ['c8', 100],
            ['c1', 100],
            ['c2', 100],
            ['c3', 100],
            ['c4', 100],
            ['c5', 100],
            ['c6', 100],
            ['c7', 100],
            ['c8', 100],
            ['c1', 100],
            ['c2', 100],
            ['c3', 100],
            ['c4', 100],
            ['c5', 100],
            ['c6', 100],
            ['c7', 100],
            ['c8', 100],
            ['c1', 100],
            ['c2', 200],
            ['c3', 300],
            ['c4', 400],
            ['c5', 400],
            ['c6', 400],
            ['c7', 400],
            ['c8', 400],
            ['c1', 400],
            ['c2', 400],
            ['c3', 400],
            ['c4', 400],
            ['c5', 400],
            ['c6', 400],
            ['c7', 400],
            ['c8', 400]
          ],
          step2: [ //转速控制
            ['c1', 0], // 第一个元素特殊设置
            ['c1', 200],
            ['c2', 180],
            ['c3', 160],
            ['c4', 140],
            ['c5', 120],
            ['c6', 100],
            ['c7', 80],
            ['c8', 60],
            ['c1', 60],
            ['c2', 60],
            ['c3', 60],
            ['c4', 60],
            ['c5', 60],
            ['c6', 60],
            ['c7', 60],
            ['c8', 60],
            ['c1', 70],
            ['c2', 80],
            ['c3', 90],
            ['c4', 100],
            ['c5', 110],
            ['c6', 120],
            ['c7', 130],
            ['c8', 140],
            ['c1', 150],
            ['c2', 160],
            ['c3', 170],
            ['c4', 180],
            ['c5', 190],
            ['c6', 200],
            ['c7', 200],
            ['c8', 200],
            ['c1', 200],
            ['c2', 200],
            ['c3', 200],
            ['c4', 200],
            ['c5', 200],
            ['c6', 200],
            ['c7', 200],
            ['c8', 200],
            ['c1', 200],
            ['c2', 200],
            ['c3', 200],
            ['c4', 200],
            ['c5', 200],
            ['c6', 200],
            ['c7', 200],
            ['c8', 200]
          ],
        },
        methods: {
          toPrizeRecord: function() { //中奖记录跳转
            location.href = '../html/FPtPrizeRecord.html';
          },
          toPrizeChance: function() { //中奖记录跳转
            location.href = '../html/FPtPrizeChance.html';
          },
          getPrize: function() { //抽奖判断
            this.initStatus = 0;
            if (this.number == 0) {
              this.initStatus = 1;
            } else if (this.prizeSwitch == 1) {
              this.prizeSwitch = 2;
              ajaxTemplate(addressTest + SERVICE.luckDrow, function(data) {
                console.log(data);
                if (data.tag == 0) {
                  dataInfo.init.prizeAllInfo = data.data.result;
                  dataInfo.init.number > 0 ? dataInfo.init.number -= 1 : '';
                  dataInfo.init.prizeInfo.forEach(function(item, index) {
                    if (item.id == dataInfo.init.prizeAllInfo.id) {
                      dataNum = index + 1; //中奖产品下标
                      $('.prizeInit-box li').removeClass('cur');
                      // current == step.length ? current-- : '';
                      run(); //抽奖开始
                    };
                  });
                } else {
                  dataInfo.init.$toast(data.errMsg);
                  dataInfo.init.prizeSwitch = 1;
                };
              }, {
                userId: userId
              });
            };
          },
          initData: function() { //滚动信息拉取
            ajaxTemplate(addressTest + SERVICE.luckDrowDate, function(data) {
              console.log(data);
              if (data.tag == 0) {
                dataInfo.init.newsList = data.data.result;
                // 滚动初始化
                setTimeout(function() {
                  $('.prizeInit-info-box').FontScroll({
                    time: 2000,
                    num: 1
                  });
                }, 2000);

              } else {
                dataInfo.init.$toast(data.errMsg);
              };
            }, {});
          },
          initPrize: function() { //奖品信息查询
            ajaxTemplate(addressTest + SERVICE.queryReward, function(data) {
              console.log(data);
              if (data.tag == 0) {
                dataInfo.init.number = data.data.times;
                dataInfo.init.prizeInfo = data.data.result;
              } else {
                dataInfo.init.$toast(data.errMsg);
              };
            }, {
              userId: userId
            });
          },
          toOrderPrize: function() { //中实物 - 填地址
            setCookie('zmtMallOrder', escape(JSON.stringify({
              id: dataInfo.init.prizeAllInfo.record_id,
              remarks: dataInfo.init.prizeAllInfo.params
            })), 30);
            location.href = '../html/orderPrize.html';
          },
        }
      });
      // 数据初始化
      dataInfo.init.initPrize();
      dataInfo.init.initData();
    },
  };

template.init();
